Make sure targets always use absolute path
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 21 Dec 2016 06:22:00 +0000 (09:22 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 21 Dec 2016 11:54:33 +0000 (14:54 +0300)
src/cargo/core/manifest.rs
src/cargo/util/toml.rs
tests/metadata.rs
tests/read-manifest.rs

index d5a2f25e0f546e5a1a0124068f84ee3e5296ef97..86cd6eebd1e57850161806a6d1ae11bd32e5db45 100644 (file)
@@ -293,11 +293,12 @@ impl VirtualManifest {
 }
 
 impl Target {
-    fn blank() -> Target {
+    fn with_path(src_path: PathBuf) -> Target {
+        assert!(src_path.is_absolute());
         Target {
             kind: TargetKind::Bin,
             name: String::new(),
-            src_path: PathBuf::new(),
+            src_path: src_path,
             doc: false,
             doctest: false,
             harness: true,
@@ -309,67 +310,61 @@ impl Target {
 
     pub fn lib_target(name: &str,
                       crate_targets: Vec<LibKind>,
-                      src_path: &Path) -> Target {
+                      src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::Lib(crate_targets),
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             doctest: true,
             doc: true,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
-    pub fn bin_target(name: &str, src_path: &Path) -> Target {
+    pub fn bin_target(name: &str, src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::Bin,
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             doc: true,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
     /// Builds a `Target` corresponding to the `build = "build.rs"` entry.
-    pub fn custom_build_target(name: &str, src_path: &Path) -> Target {
+    pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::CustomBuild,
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             for_host: true,
             benched: false,
             tested: false,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
-    pub fn example_target(name: &str, src_path: &Path) -> Target {
+    pub fn example_target(name: &str, src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::Example,
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             benched: false,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
-    pub fn test_target(name: &str, src_path: &Path) -> Target {
+    pub fn test_target(name: &str, src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::Test,
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             benched: false,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
-    pub fn bench_target(name: &str, src_path: &Path) -> Target {
+    pub fn bench_target(name: &str, src_path: PathBuf) -> Target {
         Target {
             kind: TargetKind::Bench,
             name: name.to_string(),
-            src_path: src_path.to_path_buf(),
             tested: false,
-            ..Target::blank()
+            ..Target::with_path(src_path)
         }
     }
 
index e670715d4ea4fee92d548348dab83f399e41d201..b708d18cf9d09beb018c923e77384d145ea7991b 100644 (file)
@@ -29,6 +29,7 @@ pub struct Layout {
     examples: Vec<PathBuf>,
     tests: Vec<PathBuf>,
     benches: Vec<PathBuf>,
+
 }
 
 impl Layout {
@@ -549,7 +550,8 @@ impl TomlManifest {
         let new_build = self.maybe_custom_build(&project.build, &layout.root, &mut warnings);
 
         // Get targets
-        let targets = normalize(&lib,
+        let targets = normalize(&layout.root,
+                                &lib,
                                 &bins,
                                 new_build,
                                 &examples,
@@ -1090,7 +1092,8 @@ impl fmt::Debug for PathValue {
     }
 }
 
-fn normalize(lib: &Option<TomlLibTarget>,
+fn normalize(package_root: &Path,
+             lib: &Option<TomlLibTarget>,
              bins: &[TomlBinTarget],
              custom_build: Option<PathBuf>,
              examples: &[TomlExampleTarget],
@@ -1110,7 +1113,7 @@ fn normalize(lib: &Option<TomlLibTarget>,
               });
     }
 
-    fn lib_target(dst: &mut Vec<Target>, l: &TomlLibTarget) {
+    let lib_target = |dst: &mut Vec<Target>, l: &TomlLibTarget| {
         let path = l.path.clone().unwrap_or(
             PathValue::Path(Path::new("src").join(&format!("{}.rs", l.name())))
         );
@@ -1124,71 +1127,71 @@ fn normalize(lib: &Option<TomlLibTarget>,
         };
 
         let mut target = Target::lib_target(&l.name(), crate_types,
-                                            &path.to_path());
+                                            package_root.join(path.to_path()));
         configure(l, &mut target);
         dst.push(target);
-    }
+    };
 
-    fn bin_targets(dst: &mut Vec<Target>, bins: &[TomlBinTarget],
-                   default: &mut FnMut(&TomlBinTarget) -> PathBuf) {
+    let bin_targets = |dst: &mut Vec<Target>, bins: &[TomlBinTarget],
+                       default: &mut FnMut(&TomlBinTarget) -> PathBuf| {
         for bin in bins.iter() {
             let path = bin.path.clone().unwrap_or_else(|| {
                 PathValue::Path(default(bin))
             });
-            let mut target = Target::bin_target(&bin.name(), &path.to_path());
+            let mut target = Target::bin_target(&bin.name(), package_root.join(path.to_path()));
             configure(bin, &mut target);
             dst.push(target);
         }
-    }
+    };
 
-    fn custom_build_target(dst: &mut Vec<Target>, cmd: &Path) {
+    let custom_build_target = |dst: &mut Vec<Target>, cmd: &Path| {
         let name = format!("build-script-{}",
                            cmd.file_stem().and_then(|s| s.to_str()).unwrap_or(""));
 
-        dst.push(Target::custom_build_target(&name, cmd));
-    }
+        dst.push(Target::custom_build_target(&name, package_root.join(cmd)));
+    };
 
-    fn example_targets(dst: &mut Vec<Target>,
-                       examples: &[TomlExampleTarget],
-                       default: &mut FnMut(&TomlExampleTarget) -> PathBuf) {
+    let example_targets = |dst: &mut Vec<Target>,
+                           examples: &[TomlExampleTarget],
+                           default: &mut FnMut(&TomlExampleTarget) -> PathBuf| {
         for ex in examples.iter() {
             let path = ex.path.clone().unwrap_or_else(|| {
                 PathValue::Path(default(ex))
             });
 
-            let mut target = Target::example_target(&ex.name(), &path.to_path());
+            let mut target = Target::example_target(&ex.name(), package_root.join(path.to_path()));
             configure(ex, &mut target);
             dst.push(target);
         }
-    }
+    };
 
-    fn test_targets(dst: &mut Vec<Target>,
-                    tests: &[TomlTestTarget],
-                    default: &mut FnMut(&TomlTestTarget) -> PathBuf) {
+    let test_targets = |dst: &mut Vec<Target>,
+                        tests: &[TomlTestTarget],
+                        default: &mut FnMut(&TomlTestTarget) -> PathBuf| {
         for test in tests.iter() {
             let path = test.path.clone().unwrap_or_else(|| {
                 PathValue::Path(default(test))
             });
 
-            let mut target = Target::test_target(&test.name(), &path.to_path());
+            let mut target = Target::test_target(&test.name(), package_root.join(path.to_path()));
             configure(test, &mut target);
             dst.push(target);
         }
-    }
+    };
 
-    fn bench_targets(dst: &mut Vec<Target>,
-                     benches: &[TomlBenchTarget],
-                     default: &mut FnMut(&TomlBenchTarget) -> PathBuf) {
+    let bench_targets = |dst: &mut Vec<Target>,
+                         benches: &[TomlBenchTarget],
+                         default: &mut FnMut(&TomlBenchTarget) -> PathBuf| {
         for bench in benches.iter() {
             let path = bench.path.clone().unwrap_or_else(|| {
                 PathValue::Path(default(bench))
             });
 
-            let mut target = Target::bench_target(&bench.name(), &path.to_path());
+            let mut target = Target::bench_target(&bench.name(), package_root.join(path.to_path()));
             configure(bench, &mut target);
             dst.push(target);
         }
-    }
+    };
 
     let mut ret = Vec::new();
 
index 84664712339e26b84efa0e6252a0684952cd98c1..a7b63bbcea512e4b0f6346616452a7f7435cdb13 100644 (file)
@@ -27,7 +27,7 @@ fn cargo_metadata_simple() {
                             "bin"
                         ],
                         "name": "foo",
-                        "src_path": "src[/]foo.rs"
+                        "src_path": "[..][/]foo[/]src[/]foo.rs"
                     }
                 ],
                 "features": {},
@@ -341,7 +341,7 @@ const MANIFEST_OUTPUT: &'static str=
         "targets":[{
             "kind":["bin"],
             "name":"foo",
-            "src_path":"src[/]foo.rs"
+            "src_path":"[..][/]foo[/]src[/]foo.rs"
         }],
         "features":{},
         "manifest_path":"[..]Cargo.toml"
index 44f7fa6f9eae2afae0c58649f1c6b9e502834804..7aceb45451975694d9467e3524c252160357444a 100644 (file)
@@ -4,12 +4,7 @@ extern crate hamcrest;
 use cargotest::support::{project, execs, main_file, basic_bin_manifest};
 use hamcrest::{assert_that};
 
-fn remove_all_whitespace(s: &str) -> String {
-    s.split_whitespace().collect()
-}
-
-fn read_manifest_output() -> String {
-    remove_all_whitespace(r#"
+static MANIFEST_OUTPUT: &'static str = r#"
 {
     "name":"foo",
     "version":"0.5.0",
@@ -21,12 +16,11 @@ fn read_manifest_output() -> String {
     "targets":[{
         "kind":["bin"],
         "name":"foo",
-        "src_path":"src[..]foo.rs"
+        "src_path":"[..][/]foo[/]src[/]foo.rs"
     }],
     "features":{},
     "manifest_path":"[..]Cargo.toml"
-}"#)
-}
+}"#;
 
 #[test]
 fn cargo_read_manifest_path_to_cargo_toml_relative() {
@@ -38,7 +32,7 @@ fn cargo_read_manifest_path_to_cargo_toml_relative() {
                  .arg("--manifest-path").arg("foo/Cargo.toml")
                  .cwd(p.root().parent().unwrap()),
                 execs().with_status(0)
-                       .with_stdout(read_manifest_output()));
+                       .with_json(MANIFEST_OUTPUT));
 }
 
 #[test]
@@ -51,7 +45,7 @@ fn cargo_read_manifest_path_to_cargo_toml_absolute() {
                  .arg("--manifest-path").arg(p.root().join("Cargo.toml"))
                  .cwd(p.root().parent().unwrap()),
                 execs().with_status(0)
-                       .with_stdout(read_manifest_output()));
+                       .with_json(MANIFEST_OUTPUT));
 }
 
 #[test]
@@ -91,5 +85,5 @@ fn cargo_read_manifest_cwd() {
     assert_that(p.cargo_process("read-manifest")
                  .cwd(p.root()),
                 execs().with_status(0)
-                       .with_stdout(read_manifest_output()));
+                       .with_json(MANIFEST_OUTPUT));
 }